﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace water_pipe_repairing
{
    public partial class FrmMain : Form
    {
        //说明游戏对象
        private Game _game;

        public static DateTime StartTime { get; set; }

        public FrmMain()
        {
            InitializeComponent();
        }
        //获得游戏开始时间

        private void FrmMain_Load(object sender, EventArgs e)
        {
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
        }
        
        private void gamercontrolNoBlink1_Load(object sender, EventArgs e)
        {

        }

        private void Gamesarea_Paint(object sender, PaintEventArgs e)
        {
        }

        private void gamercontrolNoBlink1_Load_1(object sender, EventArgs e)
        {

        }

        private void GamesArea_Paint_1(object sender, PaintEventArgs e)
        {
            if (_game != null)//游戏已经初始化
            {
                _game.Draw(e.Graphics, this.GamesArea.Size);
                lblMapKey.Text = Convert.ToString("当前地图编号为：" + _game.mapkey);


            }
        }
        private void GamesArea_MouseDown(object sender, MouseEventArgs e)
        {
            //播放旋转音效
            SoundPlayer tipplayer = new SoundPlayer(Properties.Resources.转动);
            tipplayer.Play();
            //调用game的鼠标点击处理程序
            Block block = _game.MouseDown(e.Location, this.GamesArea.Size);
            //重绘游戏区域
            this.GamesArea.Invalidate();
            //处理输赢
            if (block != null)
            {
                //点击成功，block即点击的块
                switch (_game.IsValaveOpen(block))
                {
                    case true:
                        if (_game.IsGameOver(block))
                        //游戏结束了
                        {
                            var a = System.DateTime.Now - StartTime;
                            string timeSpan = a.TotalSeconds.ToString();
                            //计算游戏使用时间  

                            SoundPlayer successplayer = new SoundPlayer(Properties.Resources.过关);
                            successplayer.Play();
                            //播放胜利音乐

                            MessageBox.Show("水管修好了！所用时间为" + a);
                            //成功弹窗


                            _game = new Game();
                            this.GamesArea.Invalidate();
                            //初始化游戏
                        }
                        else
                        {
                            SoundPlayer successplayer = new SoundPlayer(Properties.Resources.失败);
                            successplayer.Play();

                            MessageBox.Show("水管漏水了，请你再试试吧");
                            //失败弹窗

                            _game = new Game();
                                this.GamesArea.Invalidate();                              
                        }
                        break;
                    case false:
                        break;
                }                
            }
        }

        private void btnStartGame_Click(object sender, EventArgs e)
        {
            //重置游戏
            _game = new Game();
            this.GamesArea.Invalidate();
        }

        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            FrmAboutme frm = new FrmAboutme();
            frm.ShowDialog();
        }

        private void GamesArea_Load(object sender, EventArgs e)
        {
            _game = new Game();
            StartTime = System.DateTime.Now;
            //记录游戏开始时间  
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            MessageBox.Show("点击旋转水管，当水管连接完成时点击阀门即可接通。");
        }

        private void lblMapKey_Click(object sender, EventArgs e)
        {
        }

        private void lblMapKey_Click_1(object sender, EventArgs e)
        {

        }
    }
}
